package com.dycn.dao;

import com.dycn.util.CommGson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class RedisDao {
@Autowired
private JedisPool jedisPool;

 public String getValue(String key){
	 String value=null;
	 Jedis jedis=jedisPool.getResource();
	 value=jedis.get(key);
	 jedis.close();
	 return value;
 }
	public <T> T getObject(String key,Class<T> t){
		String value=null;
		Jedis jedis=jedisPool.getResource();
		value=jedis.get(key);
        jedis.close();
		if(value==null){
            return  null;
        }else {
		 return  CommGson.getInstance().toObject(value,t);
        }
	}
 public void putValue(String key,String value){
	 Jedis jedis=jedisPool.getResource();
	 jedis.setex(key, 3, value);
	 jedis.close();
 }
	public void putSecondValue(String key,String value,int seconds){
		Jedis jedis=jedisPool.getResource();
		jedis.setex(key, seconds, value);
		jedis.close();
	}
public void putForeverValue(String key,String value){
	 Jedis jedis=jedisPool.getResource();
	 jedis.set(key,value);
	 jedis.close();
}


//模糊查询
public  Map<String,String>  getLikeValue(String likekey){
     Jedis jedis=jedisPool.getResource();
	Set<String> keys = jedis.keys(likekey);
	Map<String,String> map=new HashMap<>();
	for (String key:keys){
		map.put(key,jedis.get(key));
	}
	return  map;
}
}
